import router from '@/routes/router.js';

const state = {
	isCollapse: false,
	isEditAiFlow: false,
	menuList: [],
	// tags 数据
	tagList: [
		{
			path: '/index/home',
			name: 'home',
			meta: { title: '首页', type: '', keepAlive: true, jumpNo: 0, toPath: '/index/home' }
		}
	],
	// keepAlive 缓存数据
	cacheView: []
};
const getters = {
	getMenuList() {
		const [children] = router.filter(page => page.name === 'index');
		console.log('getMenuList ', children.children);
		return children.children;
	}
};
const mutations = {
	SET_TAG_LIST(target, clicktag) {
		const currenttag = target.tagList.find(tag => tag.name === clicktag.name);
		if (!currenttag) {
			target.tagList.push({ ...clicktag });
		}
	},

	SET_CACHE_VIEW(target, clicktag) {
		if (!target.cacheView.includes(clicktag.name)) {
			target.cacheView.push(clicktag.name);
		}
	},

	CLEAR_TAG(target, page) {
		const index = target.tagList.findIndex(tag => tag.name === page);
		target.tagList.splice(index, 1);
	},
	CLEAR_CACHE(target, name) {
		const index = target.cacheView.findIndex(tag => tag === name);
		target.cacheView.splice(index, 1);
	},
	SET_COLLAPSE(target, status) {
		target.isCollapse = status;
	},
	REPLACE_TAG_LIST(target, list) {
		target.tagList = [...list];
	}
};
const actions = {
	committag({ commit }, tags) {
		commit('SET_TAG_LIST', tags);
		commit('SET_CACHE_VIEW', tags);
	},
	cleartag({ commit }, tags) {
		commit('CLEAR_TAG', tags);
	},
	clearCache({ commit }, tags) {
		commit('CLEAR_CACHE', tags);
	}
};

export default {
	// namespaced: true,
	getters,
	state,
	actions,
	mutations
};
